package org.meeting.radish.controller;

import lombok.extern.slf4j.Slf4j;
import org.meeting.radish.entity.AnswerKey;
import org.meeting.radish.entity.Question;
import org.meeting.radish.entity.User;
import org.meeting.radish.entity.UserAnswer;
import org.meeting.radish.service.AnswerKeyService;
import org.meeting.radish.service.QuestionService;
import org.meeting.radish.service.UserAnswerService;
import org.meeting.radish.service.UserService;
import org.meeting.radish.util.CookieUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class IndexController {

    @Resource
    UserService userService;

    @Resource
    UserAnswerService userAnswerService;


    @Resource
    AnswerKeyService answerKeyService;

    @GetMapping("/")
    public String index(Map<String, Object> map,
                        @CookieValue(value = "uid", defaultValue = "-1") Integer uid,
                        @CookieValue(value = "status", defaultValue = "-2") String status,
                        HttpServletResponse resp) {
        List<AnswerKey> answerKeys = answerKeyService.queryAllAnswerKeyByUid(uid);
        List<UserAnswer> userAnswers = userAnswerService.queryUserAnswerByUid(uid);
        List<User> users = userService.queryUserRequest();

        map.put("answerKeys", answerKeys);
        map.put("userAnswers", userAnswers);
        map.put("users", users);
        if (status.equals("1") || status.equals("-1") || status.equals("0")) {
            map.put("status", Integer.parseInt(status));
        } else {

            CookieUtil.addCookie("uid", "-1", resp);
            CookieUtil.addCookie("status", "-2", resp);
            map.put("status", -2);
        }
        return "index";
    }
}
